package com.examples.ry.graphics;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ExperimentImage2 {

	public static void main(String args[]) {

		final ImageLoader loader = new ImageLoader();
		final String str = "images.gif";
		System.out.println("loading");
		new Thread(new Runnable() {
			boolean loading = false;
			int lastImageIndex = 0;

			public void run() {
				try {
					ImageData[] answer = loader.load(str);
					if (answer == null)
						return;
					Image[] images = new Image[answer.length];
					lastImageIndex = answer.length - 1;
					for (int i = 0; i <= lastImageIndex; ++i)
						updateImage(i, answer[i]);
				} catch (Throwable t) {
					System.out.println(t.toString());
				} finally {
					loading = false;
					System.out.println("finished");
				}
			}
		}).start();
	}

	private static void updateImage(int index, ImageData imageData) {
		final Display display = new Display();
		Shell shell = new Shell(display, SWT.SHELL_TRIM);
		final Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND
				| SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);

		final int idx = index;
		final ImageData id = imageData;
		display.syncExec(new Runnable() {
			int x = 0;

			public void run() {
				// images[idx] = new Image(display, id);
				x = x + 10;
				canvas.redraw(x, 0, id.width, id.height, false);
				canvas.update();
			}
		});
		shell.open();
		shell.setSize(500,500);
	}
}
